#include <unistd.h>
#include <iostream>
#include "dal_dmabufheap/DmaBufHeapManager.hpp"

int main(int argc, char* argv[])
{
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <size>" << std::endl;
        return 1;
    }

    bool ret;
    int size = std::stoi(argv[1]);

    dal_dmabufheap::DmaBufHeapManager* h = dal_dmabufheap::DmaBufHeapManager::GetInstance();
    dal_dmabufheap::DmaBufHeapManager::Block* b = new dal_dmabufheap::DmaBufHeapManager::Block();

    ret = h->CreateBlock(size, b);
    if (!ret) {
        std::cerr << "CreateBlock failed" << std::endl;
        return 1;
    }

    std::cout << "Created block succeed, size = " << b->size 
              << ", global_fd = " << b->global_fd
              << ", ptr = " << b->ptr
              << std::endl;

    char start[5] = {'1', '2', '3', '4', '5'};
    char end[5] = {'a', 'b', 'c', 'd', 'e'};
    ret = h->WriteAndSyncBlock(b, 0, start, 5);
    if (!ret) {
        std::cerr << "WriteAndSyncBlock failed" << std::endl;
        return 1;
    }
    ret = h->WriteAndSyncBlock(b, b->size - 5, end, 5);
    if (!ret) {
        std::cerr << "WriteAndSyncBlock failed" << std::endl;
        return 1;
    }

    std::cout << "WriteAndSyncBlock succeed, in block, "
              << "first 5 bytes are 12345 and last 5 bytes are abcde" << std::endl;
    int cnt = 10;
    while (cnt--) {
        sleep(1);
    }

    h->DestroyBlock(b);
}
